# -*-coding:utf-8-*-
from selenium.webdriver.common.by import By
import time
from LbtFront.Base.base import Page
from Resources import UserName
from DriverAndUnit import myunit,myfunction
from time import sleep
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class login(Page):
    """
    用户登录
    """
    url='/'
    #登录用户名
    def login_username(self,username):
        self.driver.find_element(By.ID,'username').clear()
        self.driver.find_element(By.ID,'username').send_keys(username)
    #登录密码
    def login_password(self,password):
        self.driver.find_element(By.ID,'password').clear()
        self.driver.find_element(By.ID,'password').send_keys(password)
    #登录按钮
    def login_button(self):
        self.driver.find_element(By.ID, 'btnLogin').click()
    #切换幼儿园管理
    def login_switch_schoolMange(self):
        #self.driver.find_element(By.LINK_TEXT,'幼儿园管理').click()
        self.driver.find_element_by_xpath("//a[contains(@href,'login.jhtml')]").click()
    # 统一登录入口
    def user_login(self, username, password):
        '''获取用户名和密码登录'''
        self.open()
        #self._login()
        self.login_switch_schoolMange()
        self.login_username(username)
        self.login_password(password)
        self.login_button()
        time.sleep(2)
    #选一个学校
    def choiceSchool(self,btn_rolse='',schoolname=''):
        allRoles=self.driver.find_elements(By.CLASS_NAME,btn_rolse)
        for z in allRoles:
            if z.text == schoolname:
                z.click()
                break
    #验证登录成功的用户名密码
    def user_login_success(self):
        return self.driver.find_element(By.CLASS_NAME,'name').text


    def login_true(self):
        self.user_login(UserName.lbtUserName, UserName.lbtUserPassword)
        sleep(3)
        '''选择学校，并登录，且验证登录后的用户名是否正确，并拍照'''
        self.choiceSchool('btn_rolse', '乐贝通幼儿园')
        '''print(po.user_login_success())'''
        self.assertEqual(self.user_login_success(), UserName.lbtUserNameInfo)
        myfunction.insert_img(self.driver, 'test_loginSuccess.png')
    def user_login_success(self):
        return self.driver.find_element(By.CLASS_NAME,'name').text